package com.lanou.dllo.vc.activity;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMContactListener;
import com.easemob.chat.EMContactManager;
import com.lanou.dllo.vc.R;
import com.lanou.dllo.vc.adapter.AddFriendMessageAdapter;
import com.lanou.dllo.vc.db.InviteMessgeDao;
import com.lanou.dllo.vc.entity.InviteMessage;
import com.lanou.dllo.vc.receiver.ChatBroadcast;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class AddFriendMessage extends AppCompatActivity {
    @Bind(R.id.list)
    ListView list;
    @Bind(R.id.backIv)
    ImageView backIv;

    private String username = null;


    @Override
    public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
        ChatBroadcast msgReceiver = new ChatBroadcast();
        IntentFilter intentFilter = new IntentFilter(EMChatManager.getInstance().getNewMessageBroadcastAction());
        intentFilter.setPriority(3);
        return super.registerReceiver(msgReceiver, intentFilter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_friend_message);

        ButterKnife.bind(this);
        Intent intent = getIntent();
        if (intent != null) {
            if (intent.getStringExtra("username") != null && intent.getStringExtra("username").length() > 0) {
                username = intent.getStringExtra("username");
                Log.i("lanou", username + "------->");
            }
        }

        if (username != null && username.length() > 0) {
            InviteMessage inviteMessage = new InviteMessage();

            inviteMessage.setFrom(username);
            InviteMessgeDao dao = new InviteMessgeDao(this);

            List<InviteMessage> mesg = dao.getMessagesList();
            if (!mesg.contains(inviteMessage)) {
                mesg.add(inviteMessage);
            }

            AddFriendMessageAdapter adapter = new AddFriendMessageAdapter(this, 1, mesg);
            list.setAdapter(adapter);
            dao.getUnreadMessagesCount();
        }

        backIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


    }
}
